package tuiLayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;

/**
 * Write a description of class DVDMenuUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDUI
{
    private DvdCtr dvdCtr;
    private String command;

    public DVDUI()
    {
        dvdCtr = new DvdCtr();
    }
    
    public void start()
    {
        boolean finished = false;
        while (!finished)
        {
            printMenu();
            reader();
            if(command.equals("1"))
            {
                System.out.println("Returning to main Menu.");
                finished = true;
            }
            else if(command.equals("help"))
                ;
            else if(command.equals("2"))
                create();
            else if(command.equals("3"))
                update();
            else if(command.equals("4"))
                delete();
            else if(command.equals("5"))
                listOfDvds();

        }
    }

    private void reader()
    {
        final String[] validCommands = {"1","2","3","4","5", "help",};
        Scanner reader;
        reader = new Scanner(System.in);
        boolean finished = false;
        while(!finished)
        {
            System.out.println();
            System.out.println("> ");
            String terminal = reader.nextLine();
            System.out.println();
            for(int i = 0; i < validCommands.length; i++) 
            {
                if(validCommands[i].equals(terminal))
                {
                    command = terminal;
                    finished = true;
                }
            }
            if(finished == false)
                System.out.println("Try once again.");
        }
    }

    private String reader2()
    {
        Scanner terminal = new Scanner(System.in);
        System.out.println(">> ");
        String command2 = terminal.nextLine();
        return command2;
    }

    private void printMenu()
    {
        System.out.println("");
        System.out.println("*** DVD Menu ***");
        System.out.println("(1) Return to main Menu.");
        System.out.println("(2) Create new DVD.");
        System.out.println("(3) Update DVD/Copy Menu.");
        System.out.println("(4) Delete DVD.");
        System.out.println("(5) List of the dvds");
        System.out.println("Please select using the keypad");
    }

    private void create()
    {
        System.out.println("\f");
        System.out.println("Type in:");
        System.out.println("barcode");
        String barcode = reader2();
        System.out.println("title");
        String title = reader2();
        System.out.println("artist");
        String artist = reader2();
        System.out.println("publication date");
        String publicationDate = reader2();
        System.out.println("\f");
        System.out.println("* New DVD created. *");

        dvdCtr.createDvd(barcode, title, artist, publicationDate);
    }

    private void createCopy(String dvdTitle)
    {
        System.out.println("\f");
        if(dvdCtr.findDvd(dvdTitle) !=null)
        {
             System.out.println("* DVD Creation *");
            System.out.println("Type in:");
            System.out.println("Serial number");
            String serialNumber = reader2();
            System.out.println("Purchase date");
            String purchaseDate = reader2();
            System.out.println("Purchase price");
            String purchasePrice = reader2();
            if(dvdCtr.createCopy(dvdTitle, serialNumber, purchaseDate, purchasePrice)==1)
            {
                System.out.println("\f");
                System.out.println("* The copy was created *");
                System.out.println("");
            }
            else
            {
                System.out.println("#############");
                System.out.println("Dvd not found");
                System.out.println("#############");
                System.out.println("");
            }
        }
    }

    private void copyMenu(String dvdTitle)
    {
        System.out.println("\f");
        boolean finished = false;
        while (!finished)
        {
            System.out.println("*** Copy Menu ***");
            System.out.println("(1) Return to DVD update Menu.");
            System.out.println("(2) Create copy.");
            System.out.println("(3) Update copy.");
            System.out.println("(4) Delete copy.");
            System.out.println("(5) Copy information");
            System.out.println("Please select using the keypad");
            reader();
            if(command.equals("1"))
            {
                System.out.println("\f");
                System.out.println("Returning to Dvd Menu.");
                finished = true;
            }
            else if(command.equals("help"))
                update();
            else if(command.equals("2"))
                createCopy(dvdTitle);
            else if(command.equals("3"))
                updateCopy(dvdTitle);
            else if(command.equals("4"))
                removeCopy(dvdTitle);
            else if(command.equals("5"))
                Copysdetails(dvdTitle);    
        }
    }

    private void updateCopy(String title)
    {
        System.out.println("Please enter the copy's serial number:");
        String serialNumber = reader2();
        System.out.println("\f");
        if(dvdCtr.findCopy(serialNumber, title) !=null)
        {
            boolean finished = false;
            while(!finished)
            {
                System.out.println();
                System.out.println("*** Copy Update Menu ***");
                System.out.println("(1) Return to Copy Menu.");
                System.out.println("(2) Change serial number.");
                System.out.println("(3) Change purchase date.");
                System.out.println("(4) Change purchase price.");
                System.out.println("");
                System.out.println("Please select using the keypad");
                reader();
                if(command.equals("1"))
                {
                    System.out.println("\f");
                    System.out.println("Returning to Copy Menu.");
                    finished = true;
                }
                else if(command.equals("2"))
                {
                    System.out.println("Now, type in the new serial number:");
                    String newSerialNumber = reader2();
                    dvdCtr.updateSerialNumber(title, serialNumber, newSerialNumber);
                    finished = true;
                }
                else if(command.equals("3"))
                {
                    System.out.println("Now, type in the new purchase date:");
                    String newDate = reader2();
                    dvdCtr.updatePurchaseDate(title, serialNumber, newDate);
                }
                else if(command.equals("4"))
                {
                    System.out.println("Now, type in the new purchase price:");
                    String newPrice = reader2();
                    dvdCtr.updatePurchasePrice(title, serialNumber, newPrice);
                }
            }
        }
        else
        {
            System.out.println("##############");
            System.out.println("Copy not found.");
            System.out.println("##############");
            System.out.println("");
        }
    }

    private void update()
    {
        System.out.println("Enter DVD title:");
        String titleSearch = reader2();
        System.out.println("\f");
        if(dvdCtr.findDvd(titleSearch) !=null)
        {
            System.out.println("DVD: " + dvdCtr.findDvd(titleSearch).getTitle());
            boolean finished = false;
            while(!finished)
            {
                System.out.println();
                System.out.println("*** DVD Update Menu ***");
                System.out.println("(1) Return to DVD Menu.");
                System.out.println("(2) Change title.");
                System.out.println("(3) Copy Menu.");
                System.out.println("Please select using the keypad");
                reader();
                if(command.equals("1"))
                {
                    System.out.println("\f");
                    System.out.println("Returning to DVD Menu.");
                    finished = true;
                }
                else if(command.equals("2"))
                {
                    System.out.println("Now, type in the new title:");
                    String newTitle = reader2();
                    dvdCtr.setTitle(newTitle, titleSearch);
                    System.out.println("\f");
                    System.out.println("* Title changed *");
                    System.out.println("");
                    finished =true;
                }
                else if(command.equals("3"))
                {
                    System.out.println("Update copy:");
                    copyMenu(titleSearch);
                }
            }
        }
        else
        {
            System.out.println("##############");
            System.out.println("DVD not found.");
            System.out.println("##############");
            System.out.println("");
        }
    }

    private void delete()
    {
        System.out.println("Enter DVD title:");
        String dvdToDelete = reader2();
        boolean finished = false;
        if(dvdCtr.findDvd(dvdToDelete) !=null)
        {
            System.out.println("Do you want to delete?");
            System.out.println("Type in: (1)yes or (2)no");
            reader();
            if(command.equals("1"))
            {
                System.out.println("\f");
                System.out.println("* DVD was deleted *");
                dvdCtr.removeDvd(dvdToDelete);
                finished = true;
            }
            else if(command.equals("2"))
            {
                System.out.println("\f");
                System.out.println("* DVD wasn't deleted *");
            }
        }
        else
        {
            System.out.println("\f");
            System.out.println("#######################");
            System.out.println("Could not find the DVD");
            System.out.println("#######################");
            System.out.println("");
        }
    }

    private void removeCopy(String title)
    {
        System.out.println("Please enter the Copy's serial number:");
        String copyToDelete = reader2();
        boolean finished = false;
        if(dvdCtr.findCopy(copyToDelete, title) !=null)
        {
            System.out.println("Do you want to delete?");
            System.out.println("Type in: (1)yes or (2)no");
            reader();
            if(command.equals("1"))
            {
                System.out.println("* Copy was deleted *");
                dvdCtr.removeCopy(copyToDelete, title);
                finished = true;
            }
            else if(command.equals("2"))
            {
                System.out.println("* Copy wasn't deleted *");
            }
        }
        else
        {
            System.out.println("########################");
            System.out.println("Could not found the Copy");
            System.out.println("########################");
            System.out.println("");
        }
    }

    private void listOfDvds()
    {
        System.out.println("\f");
        System.out.println("** DVD List **");
        int z = 1;
        for (Dvd Dvdobj: dvdCtr.getList())
        {
            System.out.println("Title "+ z +": " + Dvdobj.getTitle());
            z++;
        }

    }

    private void Copysdetails(String title)
    {
        System.out.println("\f");
        Dvd Dvdobj = dvdCtr.findDvd(title);

        for (Copy CopyObj: Dvdobj.getList())
        {
            System.out.println("------------------");
            System.out.println("Title: " + title );
            System.out.println("SerialNumber: " + CopyObj.getSerialNumber());
            System.out.println("PurchaseDate: " + CopyObj.getPurchaseDate());
            System.out.println("PurchasePrice: " + CopyObj.getPurchasePrice());
            System.out.println("------------------\n");
        }

    }
}
